package com.kunbo.cn.controller.system.user;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kunbo.cn.dao.system.user.DepartmentDao;
import com.kunbo.cn.entity.system.user.Department;
import com.kunbo.cn.service.system.user.DepartmentService;
import com.kunbo.cn.util.json.Json;
import com.kunbo.cn.util.retu.ReturnBean;

@Controller  
@RequestMapping("/system/user/department/*")  
public class DepartmentController {
	

    @Resource 
    private DepartmentService departmentService;
    
	@Autowired
	private DepartmentDao departmentDao;
	
    @RequestMapping(value="getAll.do")  
    public @ResponseBody List<Department> login(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		return departmentService.getAll(0);
    }
    
    
    @RequestMapping(value="save.do")  
    public @ResponseBody ReturnBean save(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String data = request.getParameter("data").replaceAll("\"\"", "null");
    		Department userStatus = (Department) Json.toObject(Department.class, data);
    		departmentDao.save(userStatus);
    		return new ReturnBean(true,"");
    }
    
    @RequestMapping(value="delete.do")  
    public @ResponseBody ReturnBean delete(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String data = request.getParameter("data").replaceAll("\"\"", "null");
    		Department userStatus = (Department) Json.toObject(Department.class, data);
    		departmentDao.delete(userStatus);
    		return new ReturnBean(true,"");
    }
    //根据id删除部门
    @RequestMapping(value="deleteById.do")  
    public @ResponseBody ReturnBean deleteDepartById(int id) {
    		departmentDao.delete(id);
    		return new ReturnBean(true,"");
    }
    
    
    @RequestMapping(value="getOne.do")  
    public @ResponseBody Department getOne(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
    		String id = request.getParameter("id");
    		return departmentDao.findById(Integer.parseInt(id));
    }
    
}
